#include "client_h.h"

void play_client(){
    printf("*********************************************************\n");
    printf("*               1.注册            2.登录               3.退出              *\n");
    printf("*********************************************************\n");
    printf("请输入选择:");
}

void do_register(int sockfd,MSG* msg){
    msg->type=R;

    printf("请输入名称:");
    fgets(msg->name,32,stdin);
    msg->name[strlen(msg->name)-1]='\0';

    printf("请输入密码:");
    fgets(msg->pass,32,stdin);
    msg->pass[strlen(msg->pass)-1]='\0';

    if(send(sockfd,msg,sizeof(MSG),0)==-1){
        ERRLOG("send error");
    }

    if(recv(sockfd,msg,sizeof(MSG),0)==-1){
        ERRLOG("recv error");
    }

    if(strncmp(msg->text,"KO",2)!=0){
        printf("名称已经存在le\n");
        return ;
    }
    
    printf("注册成功\n");

}

int do_login(int sockfd,MSG* msg){
            msg->type=L;

            printf("请输入名称:");
            fgets(msg->name,32,stdin);
            msg->name[strlen(msg->name)-1]='\0';

            printf("请输入密码:");
            fgets(msg->pass,32,stdin);
            msg->pass[strlen(msg->pass)-1]='\0';

            if(send(sockfd,msg,sizeof(MSG),0)==-1){
                ERRLOG("send error");
            }

            if(recv(sockfd,msg,sizeof(MSG),0)==-1){
                ERRLOG("recv error");
            }

            if(strncmp(msg->text,"KO",2)!=0){
                printf("登录失败\n");
                return 0;
            }
            
            printf("登录成功\n");

            return 1;
}

void do_next_login(int sockfd,MSG* msg){
    int n;
    while(1){
        play_next_client();
        if(scanf("%d",&n)!=1){
            printf("输入格式错误\n");
            while(getchar()!='\n');
            continue;
        }
        getchar();

        if(n==0){
            break;
        }
        
        switch(n){
                case 1:
                            do_select(sockfd,msg);
                            break;
                case 2:
                            do_history(sockfd,msg);
                            break;
                case 3:
                            close(sockfd);
                            exit(0);
                            break;
                default:
                            printf("非法命令\n");
            }
        }
}

void play_next_client(){
    printf("*********************************************************\n");
    printf("*               1.查询            2.历史               3.退出              *\n");
    printf("*********************************************************\n");
    printf("请输入选择:");
}

void do_select(int sockfd,MSG* msg){
    msg->type=S;

    while(1){
        memset(msg->text,0,sizeof(msg->text));
        printf("请输入要查询的单词:");
        fgets(msg->text,32,stdin);
        msg->text[strlen(msg->text)-1]='\0';
        if(strncmp(msg->text,"#",2)==0){
            break;
        }
        
        if(send(sockfd,msg,sizeof(MSG),0)==-1){
            ERRLOG("send error");
        }

        if(recv(sockfd,msg,sizeof(MSG),0)==-1){
            ERRLOG("recv error");
        }

        printf("%s\n",msg->text);
    }
}
                   
void do_history(int sockfd,MSG* msg){
    msg->type=H;

    if(send(sockfd,msg,sizeof(MSG),0)==-1){
        ERRLOG("send error");
    }

    while(1){
        if(recv(sockfd,msg,sizeof(MSG),0)==-1){
            ERRLOG("recv error");
        }
        
        if(msg->text[0]=='\0'){
            break;
        }

        printf("%s\n",msg->text);
    }   
}